package com.app.qtjk.lsh.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.app.common.utils.CollectionUtil;
import com.app.common.utils.FileUploadUtiles;
import com.app.common.utils.ParamConfig;
import com.app.common.utils.ReqJsonUtil;
import com.app.common.utils.StringUtil;
import com.css.sword.kernel.base.annotation.Service;
import com.css.sword.kernel.base.annotation.ServiceContainer;
import com.css.sword.kernel.base.persistence.IPersistenceService;
import com.css.sword.kernel.base.persistence.SQLParam;
import com.css.sword.kernel.utils.SwordLogUtils;
import com.css.sword.kernel.utils.SwordPersistenceUtils;
import com.css.sword.platform.web.event.IReqData;
import com.css.sword.platform.web.event.IResData;
import com.css.sword.platform.web.event.SwordRes;

@ServiceContainer
public class CategoryService {

	private static SwordLogUtils log = SwordLogUtils
			.getLogger(CategoryService.class);

	public CategoryService() throws Exception {
		super();
	}

	/**
	 * 初始化分类列表
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Service(serviceName = "CategoryService_initCategoryList")
	public IResData initCategoryList(IReqData req) throws Exception {

		log.debug("开始进入 CategoryService_initCategoryList 方法。");

		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();

		IResData res = new SwordRes();
		HashMap commonMap = new HashMap();
		ArrayList commonList = new ArrayList();
		HashMap hashMap = new HashMap();
		try {
			List categoryMapList = dao.queryMapListByKey("CategoryManageService_initCategoryListQtjk", null);
			hashMap.put("returnStatus", "true");
			hashMap.put("msg", "成功返回，未出现异常！");
			commonMap.put("commonData", hashMap);
			commonMap.put("categoryMapList", categoryMapList);
			commonList.add(commonMap);
			res.addListWithDataName("returnData", commonList);
		} catch (Exception e) {
			log.debug("---------------系统发生异常！---------------");
			e.printStackTrace();
			hashMap.put("returnStatus", "false");
			hashMap.put("msg", "出现异常！");
			commonMap.put("commonData", hashMap);
			commonList.add(commonMap);
			res.addListWithDataName("returnData", commonList);
		}
		return res;

	}

	/**
	 * 获得指定分类下面的shop列表
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Service(serviceName = "CategoryService_getBrandList")
	public IResData getBrandList(IReqData req) throws Exception {

		log.debug("开始进入 CategoryService_getBrandList 方法。");

		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();

		IResData res = new SwordRes();
		HashMap commonMap = new HashMap();
		ArrayList commonList = new ArrayList();
		HashMap hashMap = new HashMap();
		try {
			int pageNum=0;
			int pageSize=0;
			String pageSizeParamVal=ParamConfig.getInstanse().getParam("pageSize");
			SQLParam sqlParam = new SQLParam();
			StringBuffer macroparam = new StringBuffer();
			// 获取前台post的数据
			String userId = ReqJsonUtil.getPostParam(req,"userId");
			String categoryId = ReqJsonUtil.getPostParam(req,"categoryId");
			String sortType = ReqJsonUtil.getPostParam(req,"sortType");
			String pageNumStr=ReqJsonUtil.getPostParam(req,"pageNum");
			sqlParam.addParam(categoryId);
			int totalCount = Integer.parseInt(StringUtil.parse2String(dao.queryColumnByKey("CategoryService_getBrandListByCategoryId_count", sqlParam, 1)));
			if (pageNumStr.equals("")) {
				pageNum=1;
				pageSize=totalCount;
			}else {
				pageNum = Integer.parseInt(pageNumStr);
				pageSize=Integer.parseInt(pageSizeParamVal);
			}
			
			int totalPage=(totalCount%pageSize==0)?(totalCount/pageSize):((totalCount/pageSize)+1);
			int pageNumHt=(pageNum-1)*pageSize;
			
			if (sortType.equals("0")) {
				macroparam.append(" t.c_brand_sfnew DESC,s.vipsyts DESC limit "+pageNumHt+","+pageSize);
			} else if (sortType.equals("1")) {
				macroparam.append(" t.c_brand_wealth DESC limit "+pageNumHt+","+pageSize);
			} 
			/*else if (sortType.equals("2")) {
				macroparam.append(",t.c_brand_wealth ASC limit "+pageNumHt+","+pageSize);
			} else if (sortType.equals("3")) {
				macroparam.append(",t.c_brand_opptime DESC limit "+pageNumHt+","+pageSize);
			} else if (sortType.equals("4")) {
				macroparam.append(",t.c_brand_opptime ASC limit "+pageNumHt+","+pageSize);
			}*/
			sqlParam.addParam("shtg");
			sqlParam.addMacroParam(macroparam.toString());

			List<Map<String,Object>> brandMapList = dao.queryMapListByKey("CategoryService_getBrandListByCategoryId", sqlParam);
			for (int i = 0; i < brandMapList.size(); i++) {
				Map dataMap=brandMapList.get(i);
				sqlParam.clearParams();
				sqlParam.addParam(userId);
				sqlParam.addParam(StringUtil.parse2String(dataMap.get("cBrandId")));
				if (CollectionUtil.isNotBlankMap(dao.queryMapByKey("BrandService_todaySfAddWealth", sqlParam))) {
					dataMap.put("todaySfAddWealth", "true");
				}else {
					dataMap.put("todaySfAddWealth", "false");
				}
				Set<String> keySet = dataMap.keySet();
				for (String s : keySet) {
					if (s.equals("cBrandXtbpath")) {
						String webfullpath = StringUtil.parse2String(FileUploadUtiles.getFileElemenstMap(StringUtil.parse2String(dataMap.get(s))).get("WebFullPath"));
						dataMap.put("cBrandXtbpath", webfullpath);
					}
					if (s.equals("cBrandFileoldpath")) {
						dataMap.put("cBrandFileoldpath", ParamConfig.getInstanse().getParam("webDomainPath")+StringUtil.parse2String(dataMap.get(s)));
					}
				}
			}
			
			hashMap.put("totalPage", totalPage);
			hashMap.put("pageNum", pageNum);
			hashMap.put("returnStatus", "true");
			hashMap.put("msg", "成功返回，未出现异常！");
			commonMap.put("brandMapList", brandMapList);
		} catch (Exception e) {
			log.debug("---------------系统发生异常！---------------");
			e.printStackTrace();
			hashMap.put("returnStatus", "false");
			hashMap.put("msg", "出现异常！");
		}finally{
			commonMap.put("commonData", hashMap);
			commonList.add(commonMap);
			res.addListWithDataName("returnData", commonList);
		}
		return res;
	}


}